/**
 * 封装请求文件
 */

import wepy from 'wepy'

async function request (url, params, methods, titles, token) {
  let data = params || {}
  let method = methods || 'GET'
  let title = titles || '加载中...'
  let headertoken = token || false
  let res = null
  const header = {
    'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
  }
  wepy.showToast({
    icon: 'loading',
    title: title,
    duration: 10000
  })
  if (headertoken) {
    header['user-token'] = wepy.getStorageSync('token')
  }
  try {
    res = await wepy.request({
      url: url,
      data: data,
      header: header,
      method: method
    })
    wepy.hideToast()
    if (res.statusCode == 200) {
      return res.data
    } else {
      wepy.showToast({
        icon: 'loading',
        title: `服务器${res.statusCode}`
      })
      return false
    }
  } catch (e) {
    wepy.showToast({
      icon: 'loading',
      title: '请检查网络...'
    })
  }
}

export default request
